public class Test {
    public static void main(String[] args) {
        int[] arr = {2,4,4,4,0};
        System.out.println(Solution.removeElement(arr, 4));
    }
}
class Solution {
    public static int removeElement(int[] nums, int val) {
        int left = 0;
        int right = nums.length - 1;
        int count = 0;
        while(left < right) {
            while (nums[right] == val) {
                right--;
                if(right < 0) break;
            }
            if(nums[left] == val && left < right) {
                int cur = nums[left];
                nums[left] = nums[right];
                nums[right] = cur;
                right--;
            }
            left++;
        }
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] == val){
                count++;
            }
        }
        return nums.length - count;
    }
}